package com.zx.cs.controller;

import com.zx.common.annotation.Log;
import com.zx.common.core.controller.BaseController;
import com.zx.common.core.domain.AjaxResult;
import com.zx.common.core.page.TableDataInfo;
import com.zx.common.enums.BusinessType;
import com.zx.common.utils.poi.ExcelUtil;
import com.zx.cs.domain.Employee;
import com.zx.cs.service.IEmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * --- 平安喜乐 ---
 *
 * @author LiuGuowei
 * @date 2025/9/2
 * @desc 员工类Controller
 */
@RestController
@RequestMapping("cs/sm/employee")
public class EmployeeController extends BaseController {

    @Autowired
    private IEmployeeService employeeService;

    /**
     * 查询员工列表
     */
    @PreAuthorize("@ss.hasPermi('employee:list')")
    @GetMapping("/list")
    public TableDataInfo list(Employee employee) {
        startPage();
        List<Employee> list = employeeService.selectEmployeeList(employee);
        return getDataTable(list);
    }

    /**
     * 导出员工列表
     */
    @PreAuthorize("@ss.hasPermi('employee:export')")
    @Log(title = "员工", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Employee employee) {
        List<Employee> list = employeeService.selectEmployeeList(employee);
        ExcelUtil<Employee> util = new ExcelUtil<Employee>(Employee.class);
        util.exportExcel(response, list, "员工数据");
    }

    /**
     * 获取员工详细信息
     */
    @PreAuthorize("@ss.hasPermi('employee:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(employeeService.selectEmployeeById(id));
    }

    /**
     * 新增员工
     */
    @PreAuthorize("@ss.hasPermi('employee:add')") //这是一个权限注解，表示只有拥有"employee:add"（员工添加）权限的用户才能访问这个接口。
    @Log(title = "员工", businessType = BusinessType.INSERT) //这是一个日志注解，用于记录操作日志，标题为"员工"，操作类型为"INSERT"（插入），即添加员工的操作会被记录下来
    @PostMapping("/add") //这个注解表示该方法处理POST请求，路径为"/add"，用于接收添加员工的请求。
    public AjaxResult add(@RequestBody Employee employee) { //定义一个公共方法add，返回AjaxResult类型的结果。@RequestBody表示从请求体中获取Employee对象数据，即前端传递的员工信息
        return toAjax(employeeService.insertEmployee(employee)); //调用员工服务的insertEmployee方法添加员工信息，然后使用toAjax方法将操作结果（通常是影响的行数）转换为统一的Ajax响应格式并返回。
                                                                 //整个方法的作用是：添加一个新的员工信息到系统中，并返回操作是否成功的响应结果
    }

    /**
     * 修改员工
     */
    @PreAuthorize("@ss.hasPermi('employee:edit')")
    @Log(title = "员工", businessType = BusinessType.UPDATE)
    @PutMapping("/update")
    public AjaxResult edit(@RequestBody Employee employee) {
        return toAjax(employeeService.updateEmployee(employee));
    }

    /**
     * 删除员工
     */
    @PreAuthorize("@ss.hasPermi('employee:remove')")
    @Log(title = "员工", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(employeeService.deleteEmployeeByIds(ids));
    }

}
